//
//  JobsListSearchView.swift
//  GreenDeal
//
//  Created by Moennig Q on 15/12/2022.
//

import UIKit
import RxSwift
import RxCocoa
enum SearchModule: Int{
    case installations = 0
    case serviceJobs
}
class JobsListSearchView: View {
    fileprivate lazy var disposeBag = DisposeBag()
    var toDetailVCClosure: ((Int,PVDsListCellOperType?)->Void)?
    var dataObserver: BehaviorRelay<[SearchListModelProtocal]> = BehaviorRelay.init(value: [])
    let topView = SearchTopView(bgImageName: "jobs_search_top_background")
    let tableView = TableView()
    convenience init(_ searchModule: SearchModule) {
        self.init(frame: CGRect.zero)
        self.setUpView()
        self.initBinding(searchModule)
    }
    private func initBinding(_ searchModule: SearchModule){
        if searchModule == SearchModule.serviceJobs{
            //注册cell
            tableView.register(ServiceJobsTableViewCell.self, forCellReuseIdentifier: ServiceJobsTableViewCell.reuseCellIdentifier)
            //设置cell信息，绑定数据源
            dataObserver.bind(to: tableView.rx.items(cellIdentifier: ServiceJobsTableViewCell.reuseCellIdentifier,cellType: ServiceJobsTableViewCell.self)){(row, model, cell) in
                if let data = model as? ServiceJobListModel{
                    cell.setData(data)
                }
            }.disposed(by: self.disposeBag)
            
        }else if searchModule == SearchModule.installations{//installation
            //注册cell
            tableView.register(InstallationListTableViewCell.self, forCellReuseIdentifier: InstallationListTableViewCell.reuseCellIdentifier)
            //设置cell信息，绑定数据源
            dataObserver.bind(to: tableView.rx.items(cellIdentifier: InstallationListTableViewCell.reuseCellIdentifier,cellType: InstallationListTableViewCell.self)){(row, model, cell) in
                if let realModel = model as? InstallationListModel{
                    cell.setData(realModel)
                }
                //barcode
                cell.barcodeBtn.rx.controlEvent(.touchUpInside).subscribe(onNext: { [weak self] _ in
                    self?.toDetailVCClosure?(row, .barcode)
                }).disposed(by: cell.disposeBag)
                //checklist
                cell.checklistBtn.rx.controlEvent(.touchUpInside).subscribe(onNext: { [weak self] _ in
                    self?.toDetailVCClosure?(row, .checklist)
                }).disposed(by: cell.disposeBag)
                //signature
                cell.signatureBtn.rx.controlEvent(.touchUpInside).subscribe(onNext: { [weak self] _ in
                    self?.toDetailVCClosure?(row, .signature)
                }).disposed(by: cell.disposeBag)
            }.disposed(by: self.disposeBag)
        }
        //选择某一行
        tableView.rx.itemSelected.bind { [weak self] (indexPath) in
            self?.toDetailVCClosure?(indexPath.row,nil)
        }.disposed(by: disposeBag)
        //观察数据变化，刷新数据
        dataObserver.asObservable().subscribe(onNext: {[weak self] (list) in
            self?.tableView.reloadData()
        }).disposed(by: disposeBag)
    }
    override func setUpView() {
        self.backgroundColor = UIColor(hexStr: Constant.SystemBackColor)
        self.addSubview(topView)
        topView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
        self.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(topView.snp.bottom).offset(16)
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.bottom.equalToSuperview()
        }
        //
        self.tableView.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.tableView.showsVerticalScrollIndicator = false
        //防止上拉加载时数据滚动
        self.tableView.estimatedRowHeight = 200
        self.tableView.rowHeight = UITableView.automaticDimension
        
    }
}
